home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / billutil.zip / DIRSPACE.ZIP / DIRSPACE.C next >
C/C++ Source or Header  |  1993-11-13  |  2KB  |  76 lines

  1. #include <dir.h>
  2. #include <dos.h>
  3. #include <io.h>
  4. #include <string.h>
  5.  
  6. unsigned long int sum_subdir(struct ffblk, unsigned long int *);
  7.  
  8. void main(int argc, char *argv[])
  9. {
  10.     struct ffblk ffblk;
  11.     unsigned long int sum, number_space;
  12.     unsigned long int *dir_num;
  13.     int done, test;
  14.     char buffer[128], buffer2[128];
  15.  
  16.     test=666;
  17.     dir_num=&number_space;
  18.     if(argc>=2) {
  19.             getcwd(buffer2, 128);
  20.         test=chdir(argv[1]);
  21.         if(test!=0) {
  22.             printf("Not a valid directory argument.\n");
  23.             printf("Usage: dirspace [path]\n");
  24.             exit(1);
  25.         }
  26.     }
  27.  
  28.     *dir_num=1;  /* this counts the starting directory */
  29.     getcwd(buffer,128);
  30.     printf("\n%s\n",buffer);
  31.     sum=sum_subdir(ffblk, dir_num);
  32.     printf("\n%lu bytes in %lu directories.\n", sum, *dir_num);
  33.     /* if a command line arg was used */
  34.     /* we go back to the original dir */
  35.     if(test==0)
  36.         chdir(buffer2);
  37. }
  38.  
  39. unsigned long int sum_subdir(struct ffblk ffblk, unsigned long int *dir_num)
  40. {
  41.     unsigned long int sum=0;
  42.     int done, attrib, failed;
  43.     char buffer[128], dotdir[] = {'.','\0'};
  44.  
  45.     /* I think the 255 will "see" any DOS attribute because   */
  46.     /* of the hexadecimal definitions of the attribs in dir.h */
  47.     done=findfirst("*.*",&ffblk,255);
  48.     /* This checks for the . and .. entries in the dir listing. */
  49.     /* If found, they are skipped.  This is necessary because   */
  50.     /* the root dir doesn't have these, and always skipping     */
  51.     /* the first two entries misses the first two dirs in the   */
  52.         /* root directory.  Thus the necessity of this check.       */
  53.     if(strcmp(ffblk.ff_name,dotdir)==0) {
  54.         done=findnext(&ffblk);
  55.         done=findnext(&ffblk);
  56.     }
  57.     if(!done)
  58.         do {
  59.             failed=chdir(ffblk.ff_name);
  60.             /* If successful chdir returns 0 */
  61.             /* so !failed means we went into */
  62.             /* a subdirectory.               */
  63.             if(!failed) {
  64.                             getcwd(buffer, 128);
  65.                 printf("%s\n", buffer);
  66.                 (*dir_num)++;
  67.                 sum+=sum_subdir(ffblk, dir_num);
  68.                 chdir("..");
  69.                         }
  70.             sum+=ffblk.ff_fsize; /* on directories this == 0 */
  71.             done=findnext(&ffblk);
  72.         } while(!done);
  73.     return(sum);
  74. }
  75.  
  76.